package com.ruoyi.system.service.impl.processServiceImpl;


import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.process.*;
import com.ruoyi.system.mapper.process.*;
import com.ruoyi.system.service.processService.IProcessShuntService;
import com.ruoyi.system.service.processService.IProcessUnusualService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 镀铝分卷加工异常Service业务层处理
 * 
 * @author ljx
 * @date 2020-07-14
 */
@Service
public class ProcessUnusualServiceImpl implements IProcessUnusualService
{
    @Resource
    private ProcessUnusualMapper processUnusualMapper;
    @Resource
    private ProcessAluminizedRibbonMapper processAluminizedRibbonMapper;
    @Resource
    private ProcessShuntRibbonMapper processShuntRibbonMapper;
    @Resource
    private ProcessRibbonMapper processRibbonMapper;
    @Resource
    private ProcessUnusualRibbonMapper processUnusualRibbonMapper;
    @Resource
    private ProcessUnusualInventoryMapper processUnusualInventoryMapper;
    @Resource
    private ProcessInventoryMapper processInventoryMapper;
    @Resource
    private ProcessShuntInventoryMapper processShuntInventoryMapper;
    @Resource
    private ProcessAluminizedInventoryMapper processAluminizedInventoryMapper;
    @Resource
    private IProcessShuntService iProcessShuntService;
    @Resource
    private ProcessRibbonInventoryMapper processRibbonInventoryMapper;

    /**
     * 查询镀铝分卷加工异常
     * 
     * @param id 镀铝分卷加工异常ID
     * @return 镀铝分卷加工异常
     */
    @Override
    public ProcessUnusual selectProcessUnusualById(Long id)
    {
        return processUnusualMapper.selectProcessUnusualById(id);
    }

    /**
     * 查询镀铝分卷加工异常列表
     * 
     * @param processUnusual 镀铝分卷加工异常
     * @return 镀铝分卷加工异常
     */
    @Override
    public List<ProcessUnusual> selectProcessUnusualList(ProcessUnusual processUnusual)
    {
        return processUnusualMapper.selectList(processUnusual);
    }

    /**
     * 新增镀铝分卷加工异常
     * 
     * @param processUnusual 镀铝分卷加工异常
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProcessUnusual(ProcessUnusual processUnusual)
    {
        processUnusual.setCreateTime(DateUtils.getNowDate());
        String[] split = processUnusual.getInventoryIds().split(",");
        //插入异常表数据
        processUnusualMapper.insertProcessUnusual(processUnusual);
        //插入ribbon关联表数据
        if(processUnusual.getRibbonId()!=null){
            ProcessUnusualRibbon unusualRibbon = new ProcessUnusualRibbon();
            unusualRibbon.setRibbonId(processUnusual.getRibbonId());
            unusualRibbon.setUnusualId(processUnusual.getId());
            processUnusualRibbonMapper.insertProcessUnusualRibbon(unusualRibbon);
        }
        //插入inventorys关联表数据,修改inventorys表加工状态
        if(StringUtils.isNotEmpty(processUnusual.getInventoryIds())){
            List<String> list = Arrays.asList(split)
                    .stream().distinct().collect(Collectors.toList());
            //插入并卷和加工库存关联表
            processUnusualInventoryMapper.insertList(processUnusual.getId(),list);
            //修改加工库存表的加工状态
            processInventoryMapper.updateStatusString(1,list);
        }
        return 1;
    }

    @Transactional
    public int insertProcessUnusualNew(ProcessUnusual processUnusual)
    {
        processUnusual.setCreateTime(DateUtils.getNowDate());
        Long inventoryId = processUnusual.getInventoryId();
        //删除并卷和原膜的关系
        processRibbonInventoryMapper.deleteByInventoryId(inventoryId);
        //通过原膜id查询在镀铝中是否有关联关系，有记录镀铝的id，再删除镀铝的关联关系
        List<ProcessAluminizedInventory> list = processAluminizedInventoryMapper.selectAluminizedByInventoryId(Arrays.asList(inventoryId));
        if(CollectionUtils.isNotEmpty(list)){
            processUnusual.setDeleteAluminized(list.get(0).getAluminizedId());
            processAluminizedInventoryMapper.deleteInventoryByInventoryIds(inventoryId);
        }
        //通过原膜id查询在分卷中是否有关联关系，有记录分卷的id，再删除分卷的关联关系
        List<ProcessShuntInventory> list1 = processShuntInventoryMapper.selectShuntListByInventoryId(Arrays.asList(inventoryId));
        if(CollectionUtils.isNotEmpty(list1)){
            processUnusual.setDeleteShunt(list1.get(0).getShuntId());
            processShuntInventoryMapper.deleteByInventoryIds(inventoryId);
        }
        //插入异常表数据
        processUnusualMapper.insertProcessUnusual(processUnusual);
        //插入ribbon关联表数据
        if(processUnusual.getRibbonId()!=null){
            ProcessUnusualRibbon unusualRibbon = new ProcessUnusualRibbon();
            unusualRibbon.setRibbonId(processUnusual.getRibbonId());
            unusualRibbon.setUnusualId(processUnusual.getId());
            processUnusualRibbonMapper.insertProcessUnusualRibbon(unusualRibbon);
        }
        //插入inventorys关联表数据,修改inventorys表加工状态
        if(inventoryId !=null){
            ProcessUnusualInventory unusualInventory = new ProcessUnusualInventory();
            unusualInventory.setUnusualId(processUnusual.getId());
            unusualInventory.setInventoryId(inventoryId);
            //插入并卷和加工库存关联表
            processUnusualInventoryMapper.insertProcessUnusualInventory(unusualInventory);
            //修改加工库存表的加工状态
            processInventoryMapper.updateStatus(1,Arrays.asList(inventoryId));
        }
        return 1;
    }

    /**
     * 修改镀铝分卷加工异常
     * 
     * @param processUnusual 镀铝分卷加工异常
     * @return 结果
     */
    @Override
    public int updateProcessUnusual(ProcessUnusual processUnusual)
    {
        processUnusual.setUpdateTime(DateUtils.getNowDate());
        return processUnusualMapper.updateProcessUnusual(processUnusual);
    }

    /**
     * 删除镀铝分卷加工异常对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProcessUnusualByIds(String ids)
    {
        String[] array = Convert.toStrArray(ids);
        for (String s : array) {
            int type = 2;
            //首先查询到异常数据，并卷id，原膜id
            ProcessUnusual unusual = processUnusualMapper.selectProcessUnusualById(Long.valueOf(s));
            //添加并卷和员工关联关系数据
            List<String> inventIds = Arrays.asList(String.valueOf(unusual.getInventoryId()));
            processRibbonInventoryMapper.insertList(unusual.getRibbonId(),inventIds);
            //判断镀铝和分卷是否有值，如果都有值都插入关联表数据，修改原膜状态
            if(unusual.getDeleteAluminized().intValue()!=0){
                //添加镀铝和原膜关系
                processAluminizedInventoryMapper.insertList(unusual.getDeleteAluminized(),inventIds);
                type = 3;
            }
            if(unusual.getDeleteShunt().intValue()!=0){
                ProcessShuntInventory shuntInventory = new ProcessShuntInventory();
                shuntInventory.setShuntId(unusual.getDeleteShunt());
                shuntInventory.setInventoryId(unusual.getInventoryId());
                processShuntInventoryMapper.insertProcessShuntInventory(shuntInventory);
                type = 4;
            }
            //修改原膜加工状态
            processInventoryMapper.updateStatus(type,Arrays.asList(unusual.getInventoryId()));
        }
        //删除ribbon关联表数据
        processUnusualRibbonMapper.deleteByUnusualIds(array);
        //删除异常表和inventory关联表数据
        processUnusualInventoryMapper.deleteUnusualInventoryByUnusualIds(array);
        //删除异常表信息
        return processUnusualMapper.deleteProcessUnusualByIds(array);
    }

    /**
     * 删除镀铝分卷加工异常信息
     * 
     * @param id 镀铝分卷加工异常ID
     * @return 结果
     */
    @Override
    public int deleteProcessUnusualById(Long id)
    {
        return processUnusualMapper.deleteProcessUnusualById(id);
    }

    @Override
    public List<ProcessRibbon> getRibbons() {
        //获得到所有镀铝关联的并卷id
        List<Long> ribbonIds = processAluminizedRibbonMapper.selectRibbonIdAll();
        //获得到所有分卷关联的并卷id
        List<Long> ribbonIdAll = processShuntRibbonMapper.getRibbonIdAll();
        ribbonIds.addAll(ribbonIdAll);
        ribbonIds = ribbonIds.stream().distinct().collect(Collectors.toList());
        //合并获得并卷数据
        List<ProcessRibbon> ribbons = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(ribbonIds)){
            ribbons = processRibbonMapper.selectProcessRibbonByIds(ribbonIds);
            for (ProcessRibbon ribbon : ribbons) {
                List<ProcessInventory> inventories = processInventoryMapper.selectInventoryByRibbonId(ribbon.getId());
                ribbon.setInventories(inventories);
            }
        }
        return ribbons;
    }

    @Override
    public List<ProcessRibbon> getRibbonsNew() {
        //获得到异常已有的原膜编号
        List<Long> ids = processUnusualInventoryMapper.selectProcessUnusualInventoryList(null)
                .stream().map(ProcessUnusualInventory::getInventoryId).collect(Collectors.toList());
        return iProcessShuntService.getRibbonForWhere(ids);
    }

    @Override
    public List<ProcessInventory> selectInventoryByUnusualId(Long id) {
        return processInventoryMapper.selectInventoryById(id);
    }
}
